﻿using System;
using System.Data;
using System.Threading;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;
using ChinaDynastyTours.Models;
using ChinaDynastyTours.Views.FinanceGroup;

namespace ChinaDynastyTours.Views
{
    public partial class BillForm : DevExpress.XtraEditors.XtraForm
    {
        private DataTable _dtBillData;
        public string _TourgroupID { get; set; }
        //private string _ExpenseID { get; set; }


        public BillForm()
        {
            InitializeComponent();
        }

        private void AccountManagerForm_Load(object sender, EventArgs e)
        {
            tbProvider.Properties.DataSource = BillCtrl.GetProviderInfoForBill(_TourgroupID);
            
            //btnRefresh.PerformClick();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (tbProvider.EditValue == null) return;

            _dtBillData = BillCtrl.GetBillData(_TourgroupID, tbProvider.EditValue.ToString());
            billGridControl.DataSource = _dtBillData;
            btnAudit.Enabled = (_dtBillData != null && _dtBillData.Rows.Count > 0) ? true : false;

            if (_dtBillData.Rows.Count > 0)
                lblStatus.Text = _dtBillData.Rows[0]["Status"].ToString();
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {
            if (billGridView.FocusedRowHandle < 0 || tbProvider.EditValue == null) return;

            string expenseId = ExpensesCtrl.GetExpenseIDForBill(_TourgroupID, tbProvider.EditValue.ToString());
            //如果是‘地接提交’状态则可以审核
            if (ExpensesCtrl.GetAllExpenseStatusForBill(_TourgroupID, tbProvider.EditValue.ToString()))
            {
               // decimal oldBudget = 0;
               // try 
	           // {
                    //decimal newBudget = BillCtrl.GetBillSum(_TourgroupID, tbProvider.EditValue.ToString());
                    ExpensesCtrl.UpdateExpenseBudgetData(expenseId,true);
                    ExpensesCtrl.UpdateExpenseStatusData(expenseId, ExpenseStatusEnum.待审核);//审核的过程是地接提交->组团人A审核（变成待审核状态）->最终走费用状态
               // }
                /*
	            catch
	            {
                    ExpensesCtrl.UpdateExpenseBudgetData(oldBudget, expenseId);
                    ExpensesCtrl.UpdateExpenseStatusData(expenseId, ExpenseStatusEnum.地接提交);//回滚
	            } */
           
                btnRefresh.PerformClick();
            }
            else
                MessageBox.Show("当前状态不是‘地接提交’，不能审核", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void btnAuditNo_Click(object sender, EventArgs e)
        {
            if (billGridView.FocusedRowHandle < 0 || tbProvider.EditValue == null) return;

            string expenseId = ExpensesCtrl.GetExpenseIDForBill(_TourgroupID, tbProvider.EditValue.ToString());
            //如果是“待审核”或“地接提交”状态则可以审核不通过
            if (ExpensesCtrl.GetAllExpenseStatusForTravelSumbitORWaitAduit(_TourgroupID, tbProvider.EditValue.ToString()))
            {
                ExpensesCtrl.UpdateExpenseBudgetData(expenseId, false);
                ExpensesCtrl.UpdateExpenseStatusData(expenseId, ExpenseStatusEnum.计划);
                btnRefresh.PerformClick();
            }
            else
                MessageBox.Show("当前账单状态非‘地接提交’或‘待审核’，不能退审", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
